# 指定要加载的 YAML 文件路径
import os
import yaml
from pydantic import BaseModel


class SysConfig(BaseModel):
    """系统配置字典"""
    vector_store = {}
    jieba = {}
    log = {}
    volcengine_ark = {}
    qianfan = {}
    openai = {}


def init_config():
    # 指定要加载的 YAML 文件路径
    cur_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    bootstrap_path = os.path.join(cur_path, "bootstrap.yaml")
    try:
        # 使用 safe_load() 方法加载 YAML 文件
        with open(bootstrap_path, 'rb') as file:
            config = yaml.safe_load(file)
            active = config["yaml"]["profiles"]["active"]
            print("profiles.active=" + str(active))
    except IOError as e:
        print("未指定程序需要加载的配置文件，需要在bootstrap.yaml文件中配置yaml.profiles.active")

    yaml_path = os.path.join(cur_path, "llm-" + active + ".yaml")
    # print("yaml_path=" + str(yaml_path))
    with open(yaml_path, 'rb') as f:
        # 用load方法转字典
        data = yaml.load(f.read(), Loader=yaml.FullLoader)
        data["app_path"] = cur_path
        data["env"] = active
        SysConfig.vector_store = data["vector_store"] if "vector_store" in data else {}
        SysConfig.jieba = data["jieba"] if "jieba" in data else {}
        SysConfig.log = data["log"] if "log" in data else {}
        SysConfig.volcengine_ark = data["volcengine_ark"] if "volcengine_ark" in data else {}
        SysConfig.qianfan = data["qianfan"] if "qianfan" in data else {}
        SysConfig.openai = data["openai"] if "openai" in data else {}
        print("加载配置完成")


# 初始化配置
init_config()
